package nieflash.net
{
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	import flash.net.URLRequestMethod;
	import flash.text.*;
	import flash.utils.*;
	
	/**
	 * 用于打包multipart/form-data格式HTTP数据包的类
	 */
	public class MultiPartFormData
	{
		
		public var Boundary:String = "---------------------------7db1851cd1158";
		private var fieldName:String = "Content-Disposition: form-data; name=\"XXXX\"";
		private var fieldValue:String = "XXXX";
		private var fileField:String = "Content-Disposition: form-data; name=\"XXXX\"; filename=\"XXXXXXXX\"";
		private var fileContentType:String = "Content-Type: XXXX";
		private var formData:ByteArray;
		
		/**
		 * ...
		 */
		public function MultiPartFormData():void
		{
			formData = new ByteArray();
		}
		
		/**
		 * 提交
		 * @return
		 */
		public function post(_uri:String, _completeCallBack:Function = null, _progressCallBack:Function = null):void {
			var _loader:URLLoader = new URLLoader();
			var _req:URLRequest = new URLRequest(_uri);
			var _header:URLRequestHeader = new URLRequestHeader("Content-Type", "multipart/form-data; boundary=" + Boundary);
			_req.requestHeaders.push(_header);
			_req.data = GetFormData();
			_req.contentType = "application/octet-stream";
			_req.method = URLRequestMethod.POST;
			if (_completeCallBack != null) {
				_loader.addEventListener(Event.COMPLETE, _completeCallBack);
			}
			if (_progressCallBack != null) {
				_loader.addEventListener(ProgressEvent.PROGRESS, _progressCallBack);
			}
			_loader.load(_req);
		}
		
		/**
		 * 添加一个字段数据到From的数据包中
		 */
		public function AddFormField(FieldName:String, FieldValue:String):void
		{
			var newFieldName:String = fieldName;
			var newFieldValue:String = fieldValue;
			
			newFieldName = newFieldName.replace("XXXX", FieldName);
			newFieldValue = newFieldValue.replace("XXXX", FieldValue);
			
			formData.writeMultiByte("--" + Boundary + "\r\n", "UTF-8");
			formData.writeMultiByte(newFieldName + "\r\n\r\n", "UTF-8");
			formData.writeMultiByte(newFieldValue + "\r\n", "UTF-8");
		}
		
		/**
		 * 添加一个文件二进流数据到Form的数据包中，并指定二进流数据的类型
		 */
		public function AddFile(FieldName:String, FileName:String, FileContent:ByteArray, ContentType:String):void
		{
			var newFileField:String = fileField;
			var newFileContentType:String = fileContentType;
			
			newFileField = newFileField.replace("XXXX", FieldName);
			newFileField = newFileField.replace("XXXXXXXX", FileName);
			
			newFileContentType = newFileContentType.replace("XXXX", ContentType);
			
			formData.writeMultiByte("--" + Boundary + "\r\n", "UTF-8");
			formData.writeMultiByte(newFileField + "\r\n", "UTF-8");
			formData.writeMultiByte(newFileContentType + "\r\n\r\n", "UTF-8");
			
			formData.writeBytes(FileContent, 0, FileContent.length);
			
			formData.writeMultiByte("\r\n", "UTF-8");
		}
		
		/**
		 * 添加一个文件二进流数据到Form的数据包中
		 */
		public function AddStreamFile(FieldName:String, FileName:String, FileContent:ByteArray):void
		{
			AddFile(FieldName, FileName, FileContent, "application/octet-stream");
		}
		
		/**
		 * 把Form中所有的字段与二进制流数据打包成一个完整的From数据包
		 */
		public function PrepareFormData():void
		{
			formData.writeMultiByte("--" + Boundary + "--", "UTF-8");
		}
		
		/**
		 * 获得From的完整数据
		 */
		public function GetFormData():ByteArray
		{
			return formData;
		}
	
	}

}